package com.anandsoftware.security.ui.task;

import javax.swing.SwingWorker;

import com.anandsoftware.db.io.DatabaseIO;
import com.anandsoftware.db.type.DatabaseType;
import com.anandsoftware.security.SecurityAlgorithm;
import com.anandsoftware.security.SecurityUtil;
import com.anandsoftware.security.ui.SystemRegisteration;
import com.anandsoftware.service.RegistrationService;
import com.anandsoftware.service.impl.RegistrationServiceImpl;
import com.anandsoftware.system.SystemInformationUtill;
import com.anandsoftware.system.domain.SystemInformation;

public class RegisterationTask extends SwingWorker<Void, Void> {

	int progress;
	int STEP = 5;
	SystemRegisteration systemRegistration = null;
	SystemInformation systemInformation = null;
	String cdKey = "";

	public RegisterationTask(SystemRegisteration systemRegistration,
			String cdKey) {
		this.systemRegistration = systemRegistration;
		this.cdKey = cdKey;
	}

	public void initProgress(int progress) {
		this.progress = progress;
	}

	@Override
	protected Void doInBackground() throws Exception {
		setProgress(progress); // progress 25
		// fetch system info

		systemInformation = SystemInformationUtill.getSystemInformation();
		systemRegistration.setErrorIcon(false);
		systemRegistration.lblMessage.setText("Retriving System Info Done");
		progress += STEP * 4;
		setProgress(progress); // progress 45

		String username = systemRegistration.txtDbUserName.getText().trim();
		String password = new String(
				systemRegistration.txtDbPassword.getPassword()).trim();
		String dbName = systemRegistration.txtDbName.getText().trim();
		int vendorID = systemRegistration.comboVendor.getSelectedIndex();
		int vendor = 0;
		switch (vendorID) {
		case 0:
			vendor = DatabaseType.MYSQL;
			break;
		case 1:
			vendor = DatabaseType.MICROSOFT_ACCESS;
			break;
		case 2:
			vendor = DatabaseType.ORACLE;
			break;
		case 3:
			vendor = DatabaseType.SQLITE;
			break;
		}

		DatabaseIO dbIO = new DatabaseIO();
		dbIO.init(dbName, username, password, vendor);
		dbIO.createTable();
		progress += STEP + STEP;
		setProgress(progress); // progress 55

		RegistrationService regService = new RegistrationServiceImpl();
		if (regService.registerSystemInformationLocal(systemInformation, dbIO)) {
			systemRegistration.setErrorIcon(false);
			systemRegistration.lblMessage.setText("Registration Offline Done");
			progress += STEP*3;
			setProgress(progress); // progress 70
		} else {
			systemRegistration.setErrorIcon(true);
			systemRegistration.lblMessage.setText("Registration Offline Failed(#ROF)");
			return null;
		}

		// online registration start

		String key = regService.requestServiceAccess(cdKey);
		if (key.length() > 2) {
			if (regService.registerSystemInformation(systemInformation, key,
					cdKey)) {
				if (regService.registerSystemInformation(
						SecurityUtil.encryptValue(systemInformation,
								SecurityAlgorithm.SHA1), key, cdKey)) {
					systemRegistration.setErrorIcon(false);
					systemRegistration.lblMessage
							.setText("Registeration with server done");
					progress += STEP * 4;
					setProgress(progress); // progress 90
				} else {
					systemRegistration.setErrorIcon(true);
					systemRegistration.lblMessage
							.setText("Already Registered or Error(#ROEN)");
					return null;
				}
			} else {
				systemRegistration.setErrorIcon(true);
				systemRegistration.lblMessage
						.setText("Already Registered or Error(#ROPT)");
				return null;
			}
		} else {
			systemRegistration.setErrorIcon(true);
			systemRegistration.lblMessage
					.setText("Error in Connecting Server(#ROEC)");
			return null;
		}
		regService.terminateService(cdKey);
		progress += STEP + STEP;
		setProgress(progress); // progress 100
		return null;
	}
}
